<script setup>
import {listReviewsByUserId, deleteReviewById} from "@/api/review";
import {onMounted, ref} from "vue";
import {ElMessage, ElMessageBox} from "element-plus";
import {getCurrentUserInfo} from "@/api/user";
import {useRouter} from "vue-router";
import { format } from 'date-fns'

const router = useRouter()
onMounted(() => {
  getCurUserId().then(() => listReviews())
})

// ------------获取当前用户id--------------
const curUserId = ref(0)
const getCurUserId = () => {
  return getCurrentUserInfo().then(res => {
    curUserId.value = res.data.id
  })
}
// ---------根据用户ID获取评论列表--------------
const reviews = ref([])
const listReviews = () => {
  const query = {
    pageNo: pageNo.value,
    pageSize: pageSize.value,
    userId: curUserId.value
  }
  listReviewsByUserId(query).then(res => {
    reviews.value = res.data.records;
    total.value = res.data.total
  })
}
// ---------删除评论--------------
const deleteReview = (reviewId) => {
  ElMessageBox.confirm(
      '确认删除该评论?',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    deleteReviewById(reviewId).then(res => {
      if (res.data) {
        ElMessage({
          message: '删除成功',
          type: 'success'
        });
        listReviews(); // 重新加载评论列表
      } else {
        ElMessage({
          message: '删除失败',
          type: 'error'
        });
      }
    })
  })
}
// ------------前往店铺详情页--------------
const toStorePage = (id) => {
  router.push(`/user/storeDetailsPage/${id}`)
}
// --------------分页器-----------------
const pageNo = ref(1)
const pageSize = ref(10)
const total = ref(0)
const pageNoChange = (value) => {
  pageNo.value = value
  listReviews()
}
const pageSizeChange = (value) => {
  pageSize.value = value
  listReviews()
}
// ------------时间格式化--------------
const formatTime = (timeStr) => {
  if (!timeStr) return ''
  return format(new Date(timeStr), 'yyyy-MM-dd HH:mm')
}
// --------------查看评论详情--------------
const selectedReview = ref({})
const detailVisible = ref(false)
const showDetail = (review) => {
  selectedReview.value = review
  detailVisible.value = true
}
</script>

<template>
<el-card>
  <template #header>
    <div class="header">
      <span>我的评论</span>
    </div>
  </template>
  <el-table :data="reviews" style="width: 100%">
    <el-table-column label="店铺名">
      <template #default="scope">
        <span class="store-name" @click="toStorePage(scope.row.store.id)">{{scope.row.store.name}}</span>
      </template>
    </el-table-column>
    <el-table-column prop="contentText" label="评论文字" >
      <template #default="scope">
        <span :title="scope.row.contentText">
          {{ scope.row.contentText.length > 20 ? scope.row.contentText.substring(0, 20) + '...' : scope.row.contentText }}
        </span>
      </template>
    </el-table-column>
    <el-table-column label="评论时间" >
      <template #default="scope">
        {{ formatTime(scope.row.createdAt) }}
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="showDetail(scope.row)">查看详情</el-button>
        <el-button type="danger" @click="deleteReview(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-dialog v-model="detailVisible">
    <template #title>
      评论详情
    </template>
      <p><strong>店铺名:</strong> {{ selectedReview.store.name }}</p>
      <div v-if="selectedReview.contentMedia && selectedReview.contentMedia.length > 0">
        <p><strong>评论图片：</strong></p>
        <el-image
            v-if="selectedReview.contentMedia[0]"
            style="width: 100px; height: 100px; margin-right: 10px"
            :src="selectedReview.contentMedia[0]"
            :preview-src-list="selectedReview.contentMedia"
            fit="cover"
        />
        <el-image
            v-if="selectedReview.contentMedia[1]"
            style="width: 100px; height: 100px; margin-right: 10px"
            :src="selectedReview.contentMedia[1]"
            :preview-src-list="selectedReview.contentMedia"
            fit="cover"
        />
        <el-image
            v-if="selectedReview.contentMedia[2]"
            style="width: 100px; height: 100px; margin-right: 10px"
            :src="selectedReview.contentMedia[2]"
            :preview-src-list="selectedReview.contentMedia"
            fit="cover"
        />
      </div>
      <p><strong>评论内容:</strong> {{ selectedReview.contentText }} </p>
      <p><strong>评论时间:</strong> {{ formatTime(selectedReview.createdAt) }}</p>
    <template #footer>
      <el-button @click="detailVisible = false">关闭</el-button>
    </template>
  </el-dialog>
  <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="pageSizeChange"
      @current-change="pageNoChange"
  />
</el-card>
</template>

<style scoped lang="scss">
.store-name {
  cursor: pointer;
}
.store-name:hover {
  color: #409eff;
}
</style>